$(() => {
    let usp = new URLSearchParams(location.search)
    let id = usp.get('id')


    let getCategoryList = function () {
        myAjax('get', '/admin/category/list', {}, (res) => {
            let htmlStr = template('moban', res)
            $('#category').html(htmlStr)
            getArticle_edit()
        })
    }
    getCategoryList()


    let getArticle_edit = function () {
        myAjax('get', '/admin/article/search', { id }, (res) => {
            console.log(res);
            $('#inputTitle').val(res.data.title)
            $('.article_cover').prop('src', res.data.cover)
            $('#category').val(res.data.categoryId)
            $('#articleDate').val(res.data.date)
            $('#articleContent').val(res.data.content)

        })
    }

    let fuWenBen = function () {
        tinymce.init({
            selector: '#articleContent',
            language: 'zh_CN'
        });
    }
    fuWenBen()


    let shijian = function () {
        layui.use('laydate', function () {
            var laydate = layui.laydate;

            //执行一个laydate实例
            laydate.render({
                elem: '#articleDate' //指定元素
            })
        })

    }
    shijian()

    $('#form').on('submit', function (e) {
        e.preventDefault()
        let fd = new FormData(this)
        fd.append('id', id)
        fd.append('state', state)
        console.log(fd);
        myAjax('post', '/admin/article/edit', fd, (res) => {
            if (res.code !== 200) return layer.msg(res.msg)
            layer.msg(res.msg, { icon: 1, time: 1000 }, () => {
                location.href = 'article_list.html'
            })
        })
    })
    let state = ''
    $('.btn-edit').on('click', () => {
        state = '已发布'
    })

    $('#inputCover').on('change', function () {
        let url = URL.createObjectURL(this.files[0])
        $('.article_cover').prop('src', url)
    })
})